package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.CldCalendar;
import com.cleaningcloud.cld.domain.CldRestbreaksCalendarMember;
import com.cleaningcloud.cld.domain.dto.CldCalendarDto;
import com.cleaningcloud.cld.domain.dto.CldProjectPostDto;
import com.cleaningcloud.cld.domain.dto.CldRestbreaksCalendarMemberDto;
import com.cleaningcloud.cld.domain.dto.CldRestbreaksDto;
import com.cleaningcloud.cld.domain.model.TableCalendarVo;
import com.cleaningcloud.cld.domain.vo.CldCalendarVo;
import com.cleaningcloud.cld.domain.vo.CldProjectPostSetVo;
import com.cleaningcloud.cld.domain.vo.CldRestbreaksCalendarMemberVo;
import com.cleaningcloud.cld.domain.vo.CldRestbreaksVo;
import com.cleaningcloud.cld.mapper.CldProjectPostMapper;
import com.cleaningcloud.cld.mapper.CldRestbreaksCalendarMemberMapper;
import com.cleaningcloud.cld.mapper.CldRestbreaksMapper;
import com.cleaningcloud.cld.service.ICldCalendarService;
import com.cleaningcloud.cld.service.ICldRestbreaksService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.DictUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 排休管理Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-11-08
 */
@Service
public class CldRestbreaksServiceImpl implements ICldRestbreaksService {
    @Autowired
    private CldRestbreaksMapper cldRestbreaksMapper;

    @Autowired
    private CldProjectPostMapper cldProjectPostMapper;

    @Autowired
    private ICldCalendarService cldCalendarService;


    @Autowired
    private CldRestbreaksCalendarMemberMapper cldRestbreaksCalendarMemberMapper;

    /**
     * 查询排休管理
     *
     * @param restbreakId 排休管理主键
     * @return 排休管理
     */
    @Override
    public CldRestbreaksVo selectCldRestbreaksByRestbreakId(Long restbreakId) {
        // 查询项目组字典
        Map<String, String> projectGroupMap = DictUtils.getDictDataMap("cld_project_groups");
        // 查询项目岗位字典
        Map<String, String> projectPostMap = DictUtils.getDictDataMap("cld_project_post");
        CldRestbreaksVo cldRestbreaksVo = cldRestbreaksMapper.selectCldRestbreaksByRestbreakId(restbreakId);
        for (CldCalendarVo cldCalendarVo : cldRestbreaksVo.getCalendarList()) {
            for (CldRestbreaksCalendarMemberVo cldRestbreaksCalendarMemberVo : cldCalendarVo.getStaffList()) {
                if (cldRestbreaksCalendarMemberVo.getGroupName() != null) {
                    cldRestbreaksCalendarMemberVo.setGroupNames(projectGroupMap.get(cldRestbreaksCalendarMemberVo.getGroupName().toString()) + "-" + projectPostMap.get(cldRestbreaksCalendarMemberVo.getProjectPostName().toString()));
                    cldRestbreaksCalendarMemberVo.setStaffJob(cldRestbreaksCalendarMemberVo.getStaffName());
                }
            }
        }
        return cldRestbreaksVo;
    }

    @Override
    public Map<String, Object> getTableCalendarList(Long restbreakId) {
        CldRestbreaksVo cldRestbreaks = cldRestbreaksMapper.selectCldRestbreaksByRestbreakId(restbreakId);
        if (cldRestbreaks == null) {
            throw new RuntimeException("排休记录查询失败！");
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("projectName", cldRestbreaks.getProjectName());
        resultMap.put("projectId", cldRestbreaks.getProjectId());
        resultMap.put("restbreakYear", cldRestbreaks.getRestbreakYear());
        resultMap.put("restbreakMonth", cldRestbreaks.getRestbreakMonth());
        // 查询当月日历信息
        CldCalendar calendarQuery = new CldCalendar();
        calendarQuery.setCalendarYear(Math.toIntExact(cldRestbreaks.getRestbreakYear()));
        calendarQuery.setCalendayMonth(Math.toIntExact(cldRestbreaks.getRestbreakMonth()));
        List<CldCalendar> calendarList = cldCalendarService.selectCldCalendarList(calendarQuery);
        resultMap.put("calendarList", calendarList);
        List<TableCalendarVo> tableCalendarVoList = new ArrayList<>();
        // 查询项目岗位
        CldProjectPostDto projectPostQuery = new CldProjectPostDto();
        projectPostQuery.setProjectId(cldRestbreaks.getProjectId());
        projectPostQuery.setIsMasterDuits(1L);
        List<CldProjectPostSetVo> postVoList = cldProjectPostMapper.selectCldProjectPostList(projectPostQuery);
        for (CldProjectPostSetVo projectPost : postVoList) {
            if (projectPost.getStaffId() != null) {
                TableCalendarVo tableCalendarVo = new TableCalendarVo();
                tableCalendarVo.setStaffName(projectPost.getStaffName());
                tableCalendarVo.setProjectPostName(projectPost.getProjectPostName());
                tableCalendarVo.setProjectPostNo(projectPost.getProjectPostNo());
                tableCalendarVo.setGroupName(projectPost.getGroupName().toString());
                tableCalendarVo.setStaffId(projectPost.getStaffId());
                tableCalendarVo.setRestbreakId(restbreakId);
                // 根据项目岗位人员查询排休记录
                CldRestbreaksCalendarMemberDto memberQuery = new CldRestbreaksCalendarMemberDto();
                memberQuery.setRestbreaksId(restbreakId);
                memberQuery.setProjectStaffId(projectPost.getStaffId());
                Map<String, Object> memberMap = new HashMap<>();
                List<CldRestbreaksCalendarMemberVo> memberList = cldRestbreaksCalendarMemberMapper.selectCldRestbreaksCalendarMemberList(memberQuery);
                for (CldRestbreaksCalendarMemberVo member : memberList) {
                    memberMap.put(member.getCalendayDate(), member);
                }
                tableCalendarVo.setDayData(memberMap);
                tableCalendarVoList.add(tableCalendarVo);
            }

        }
        resultMap.put("staffList", tableCalendarVoList);
        return resultMap;
    }

    @Override
    public Map<String, Object> getCalendarByStaffId(CldRestbreaksDto cldRestbreaksDto) {
        Map<String, Object> resultMap = new HashMap<>();
        // 根据项目岗位人员查询排休记录
        CldRestbreaksCalendarMemberDto memberQuery = new CldRestbreaksCalendarMemberDto();
        memberQuery.setRestbreaksId(cldRestbreaksDto.getRestbreakId());
        memberQuery.setProjectStaffId(cldRestbreaksDto.getStaffId());
        List<CldRestbreaksCalendarMemberVo> memberList = cldRestbreaksCalendarMemberMapper.selectCldRestbreaksCalendarMemberList(memberQuery);
        for (CldRestbreaksCalendarMemberVo member : memberList) {
            resultMap.put("projectPostName", member.getProjectPostName());
            resultMap.put("projectPostNo", member.getProjectPostNo());
            resultMap.put("staffName", member.getStaffName());
            resultMap.put("className", member.getClassesName());
            resultMap.put(member.getCalendayDate(), member);
        }
        return resultMap;
    }

    /**
     * 查询排休管理列表
     *
     * @param cldRestbreaks 排休管理
     * @return 排休管理
     */
    @Override
    public List<CldRestbreaksVo> selectCldRestbreaksList(CldRestbreaksDto cldRestbreaks) {
        return cldRestbreaksMapper.selectCldRestbreaksList(cldRestbreaks);
    }

    /**
     * 新增排休管理
     *
     * @param cldRestbreaks 排休管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCldRestbreaks(CldRestbreaksDto cldRestbreaks) {
        // 当前项目当前年份当前月份是否已有排休数据
        CldRestbreaksDto query = new CldRestbreaksDto();
        query.setProjectId(cldRestbreaks.getProjectId());
        query.setRestbreakYear(cldRestbreaks.getRestbreakYear());
        query.setRestbreakMonth(cldRestbreaks.getRestbreakMonth());
        List<CldRestbreaksVo> isFlag = cldRestbreaksMapper.selectCldRestbreaksList(query);
        if (!isFlag.isEmpty()) {
            throw new RuntimeException("当前项目已在[" + cldRestbreaks.getRestbreakYear() + "-" + cldRestbreaks.getRestbreakMonth() + "]存在排休数据,请勿重复添加！");
        }
        cldRestbreaks.setCreateTime(DateUtils.getNowDate());
        cldRestbreaks.setIsConfirm(0L);
        cldRestbreaks.setCreateId(SecurityUtils.getUserId());
        cldRestbreaks.setCreateName(SecurityUtils.getUsername());
        cldRestbreaks.setFlag(0L);
        cldRestbreaks.setPlatform(1L);
        cldRestbreaksMapper.insertCldRestbreaks(cldRestbreaks);
        // 新增当月日历信息
        insertCalendarMember(cldRestbreaks);
        return 1;
    }

    private void insertCalendarMember(CldRestbreaksDto cldRestbreaks) {

        for (CldCalendarDto cldCalendarDto : cldRestbreaks.getCalendarList()) {
            // 批量删除
            cldRestbreaksCalendarMemberMapper.batchDeleteByCalendarIdAndRestbreakId(cldCalendarDto.getCalendarId(), cldRestbreaks.getRestbreakId());
            if (cldCalendarDto.getStaffList() != null && !cldCalendarDto.getStaffList().isEmpty()) {
                cldCalendarDto.getStaffList().forEach(item -> {
                    item.setRestbreaksStaffId(SecurityUtils.getUserId());
                    item.setRestbreaksId(cldRestbreaks.getRestbreakId());
                    item.setRestbreaksCalendarId(cldCalendarDto.getCalendarId());
                });
                cldRestbreaksCalendarMemberMapper.batchInsertRestbreaksCalendarStaff(cldCalendarDto.getStaffList());
            }
        }
    }

    /**
     * 修改排休管理
     *
     * @param cldRestbreaks 排休管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCldRestbreaks(CldRestbreaksDto cldRestbreaks) {
        // 新增当月日历信息
        insertCalendarMember(cldRestbreaks);
        return cldRestbreaksMapper.updateCldRestbreaks(cldRestbreaks);
    }

    /**
     * 批量删除排休管理
     *
     * @param restbreakIds 需要删除的排休管理主键
     * @return 结果
     */
    @Override
    public int deleteCldRestbreaksByRestbreakIds(Long[] restbreakIds) {
        return cldRestbreaksMapper.deleteCldRestbreaksByRestbreakIds(restbreakIds);
    }

    /**
     * 删除排休管理信息
     *
     * @param restbreakId 排休管理主键
     * @return 结果
     */
    @Override
    public int deleteCldRestbreaksByRestbreakId(Long restbreakId) {
        return cldRestbreaksMapper.deleteCldRestbreaksByRestbreakId(restbreakId);
    }


}
